/**
 * 生成前端路由
 */
export function generateRouters(trees: any) {
    const routers = [];
    for (const tree of trees) {
        const router: Record<string, any> = {};
        const children = tree.children;
        router.name = tree.path.substring(0, 1).toUpperCase() + tree.path.substring(1);

        if (tree.type === 3 && tree.pid === 0 && tree.is_frame === 0) {
            // 如果是目录
            router.path = `/${tree.path}`;
        } else if (tree.type === 1 && tree.pid === 0 && tree.is_frame === 0) {
            // 如果是菜单
            router.path = '/';
        } else {
            router.path = tree.path;
        }

        router.hidden = tree.visible === 0;
        if (children && children.length > 0 && tree.type === 3) {
            // 菜单类型为目录
            router.component = 'Layout';
            router.redirect = 'noRedirect';
            router.alwaysShow = true;
            router.children = generateRouters(children); // 递归
        } else {
            router.component = tree.component;
        }
        router.meta = {
            title: tree.name,
            icon: tree.icon,
            noCache: tree.is_cache === 1,
        };
        routers.push(router);
    }
    return routers;
}
